package com.tjetc.controller;

import com.tjetc.common.JsonResult;
import com.tjetc.service.AliyunSmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/sms")
public class AliyunSmsController {
    @Autowired
    private AliyunSmsService aliyunSmsService;


    /***
     *注册验证码接口
     * @param phone
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping("register")
    public JsonResult sendForRegister(String phone, HttpSession session) throws Exception {
        String send = aliyunSmsService.sendRegister(phone);
        if(send.equals("false")){
            return new JsonResult(1,"验证码获取失败，请联系管理员",send);
        }else{
            session.setAttribute("msg",send);
            return new JsonResult(0,"验证码获取成功",send);
        }
    }

    /***
     * 登录验证码接口
     * @param phone
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping("login")
    public JsonResult sendForLogin(String phone,HttpSession session) throws Exception {
        String send = aliyunSmsService.sendLogin(phone);
        if(send.equals("false")){
            return new JsonResult(1,"验证码获取失败，请联系管理员",send);
        }else{
            session.setAttribute("msg",send);
            return new JsonResult(0,"验证码获取成功",send);
        }
    }

    /***
     * 身份验证验证码接口
     * @param phone
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping("identityAuthentication")
    public JsonResult sendForIdentityAuthentication(String phone,HttpSession session) throws Exception {
        String send = aliyunSmsService.identityAuthentication(phone);
        if(send.equals("false")){
            return new JsonResult(1,"验证码获取失败，请联系管理员",send);
        }else{
            session.setAttribute("msg",send);
            return new JsonResult(0,"验证码获取成功",send);
        }
    }


}
